<template>
    <uv-popup ref="popup" round="14">
        <view class="popup">
            <view class="new-top">检测到新版本</view>
            <view class="new-intro">
                <view>版本：{{ vCo.Version ? vCo.Version : '' }}</view>
                <view>
                    更新内容：{{ vCo.Description ? vCo.Description : '' }}
                </view>
            </view>
            <view class="bom-btn">
                <view @click="changeUpdate(false)">暂不更新</view>
                <view>|</view>
                <view class="yg" @click="changeUpdate(true)">更新</view>
            </view>
        </view>
    </uv-popup>
</template>

<script setup lang="ts">
import { VerCheck_API } from '@/server/home'

const vCo = ref<any>({})
const popup = ref()
const update = async () => {
    const appid = import.meta.env.LH_ADMINCODE
    // #ifdef APP-PLUS
    plus.runtime.getProperty(plus.runtime.appid, async (wgtinfo) => {
        const code = wgtinfo.versionCode
        const res = await VerCheck_API({
            appid,
            apptype: '1',
            ver: code,
        })
        if (res.StatusCode === 0) {
            const val = res.Data
            if (val === undefined) {
                uni.showToast({
                    title: '当前为最新版本',
                    icon: 'none',
                })
            } else {
                vCo.value = val
                popup.value.open()
            }
        }
    })
    // #endif
    // #ifdef H5
    const code = uni.getSystemInfoSync().appVersionCode || '20250505';
    const res = await VerCheck_API({ appid, apptype: '1', ver: code })
    if (res.StatusCode === 0) {
        const val = res.Data
        if (val === undefined) {
            uni.showToast({
                title: '当前为最新版本',
                icon: 'none',
            })
        } else {
            vCo.value = val
            popup.value.open()
        }
    }
    // #endif
}
defineExpose({
    update,
})
const changeUpdate = (type) => {
    if (type) {
        // #ifdef APP-PLUS
        const dtask = plus.downloader.createDownload(
            vCo.value.Url,
            {},
            (d, status) => {
                // 下载完成
                if (status == 200) {
                    uni.hideLoading()
                    plus.runtime.install(
                        plus.io.convertLocalFileSystemURL(d.filename),
                        {
                            force: true,
                        },
                        () => {
                            plus.runtime.restart()
                        },
                        () => {
                            uni.showToast({
                                title: '安装失败',
                                mask: false,
                                duration: 1500,
                            })
                        },
                    )
                } else {
                    uni.hideLoading()
                    uni.showToast({
                        title: '更新失败',
                        mask: false,
                        duration: 1500,
                    })
                }
            },
        )
        uni.showLoading({
            title: '更新中...',
        })
        dtask.start()
        // #endif
        popup.value?.close()
    } else {
        popup.value?.close()
    }
}
</script>

<style lang="scss" scoped>
.popup {
    width: 600rpx;
}

.new-top {
    padding: 20rpx;
    text-align: center;
    font-size: 32rpx;
    font-weight: bold;
    border-bottom: solid 2rpx #b8b8b8;
}

.new-intro {
    height: 300rpx;
    padding: 20rpx;
    border-bottom: solid 2rpx #b8b8b8;
}

.bom-btn {
    display: flex;
    padding: 20rpx;
    align-items: center;
    justify-content: space-evenly;
    text-align: center;
    font-size: 32rpx;

    .yg {
        color: #2979ff;
    }
}
</style>
